package com.itheima.travel.service.impl;

import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.AffixMapper;
import com.itheima.travel.pojo.Affix;
import com.itheima.travel.pojo.AffixExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.RedisCacheService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class AffixServiceImpl implements AffixService {
    @Autowired
    private AffixMapper affixMapper;

    @Value("upLoad.pathRoot")
    String pathRoot;

    @Value("upLoad.webSite")
    String webSite;

    @Autowired
    private RedisCacheService redisCacheService;

    @Override
    public AffixVo upLoad(MultipartFile multipartFile, AffixVo affixVo) throws IOException {
        if(multipartFile==null){
            return null;
        }
        String businessType = affixVo.getBusinessType();
        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        affixVo.setSuffix(suffix);
        String fileName = String.valueOf(UUID.randomUUID().toString());
        affixVo.setFileName(fileName);
        String pathUrl = businessType + "/" + fileName + suffix;
        File file = new File(pathRoot + pathUrl);
        multipartFile.transferTo(file);
        pathUrl = webSite+pathUrl;
        affixVo.setPathUrl(pathUrl);
        affixMapper.insert(BeanConv.toBean(affixVo, Affix.class));
        return affixVo;
    }

    @Override
    public Boolean bindBusinessId(AffixVo affixVo) {
//        Affix affix = BeanConv.toBean(affixVo, Affix.class);
//        int i = affixMapper.updateByPrimaryKey(affix);
//        return i>0;
        Affix affix = BeanConv.toBean(affixVo, Affix.class);
        int rows = affixMapper.updateByPrimaryKeySelective(affix);
        boolean flag = rows>0;
        if (flag){
            //更新删除缓存
            String key = RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID +affixVo.getBusinessId();
            redisCacheService.deleListCache(key);
        }
        return flag;
    }

    @Override
    public List<AffixVo> findAffixByBusinessId(AffixVo affixVo) {
//        AffixExample affixExample = new AffixExample();
//        affixExample.createCriteria().andBusinessIdEqualTo(affixVo.getBusinessId());
//        List<Affix> affixes = affixMapper.selectByExample(affixExample);
//        return BeanConv.toBeanList(affixes,AffixVo.class);
        //为每个主业务添加缓存
        String key = RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID +affixVo.getBusinessId();
        return redisCacheService.listCache(()->{
            AffixExample example = new AffixExample();
            example.createCriteria().andBusinessIdEqualTo(affixVo.getBusinessId());
            List<Affix> affixes = affixMapper.selectByExample(example);
            return BeanConv.toBeanList(affixes, AffixVo.class);
        }, key);
    }
}
